package com.dongdongshop.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.dk.data.R;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.service.TbcontentCateService;
import com.dongdongshop.service.TbcontentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

//广告管理
@Controller
@RequestMapping("tbcont")
public class TbcontentController {


    @DubboReference
    TbcontentService tts;

    @DubboReference
    TbcontentCateService ttcs;

    @Autowired
    RedisTemplate redisTemplate;


    //跳转页面
    @RequestMapping("tiao")
    public String tiao(Model mo){
        List<TbContentCategory> t = ttcs.getall();
        mo.addAttribute("t",t);
        return "admin/content";
    }



    //查询全部
    @RequestMapping("getall")
    @ResponseBody
    public R<List<TbContent>> getall(){
        List<TbContent> tbcontent = tts.getall();
        return R.ok().setData(tbcontent);
    }

    //增加
    @RequestMapping("addcont")
    @ResponseBody
    public R addcont(TbContent tc){
        redisTemplate.delete("lunbo");//轮播图
        redisTemplate.delete("tuijian"+2);//推荐
        redisTemplate.delete("like"+4);//喜欢
        tts.addcont(tc);
        return R.ok();
    }


    //修改回显
    @RequestMapping("upbyid")
    @ResponseBody
    public R<TbContent> upbyid (Long id){
        TbContent tbcontent = tts.upbyid(id);
        return R.ok().setData(tbcontent);
    }


    //修改
    @RequestMapping("upcont")
    @ResponseBody
    public R upcont(TbContent tc){
        redisTemplate.delete("lunbo");//轮播图
        redisTemplate.delete("tuijian"+2);//推荐
        redisTemplate.delete("like"+4);//喜欢
        tts.upcont(tc);
        return R.ok();
    }

    //删除
    @RequestMapping("deleteid")
    @ResponseBody
    public R deleteid(String ids){
        redisTemplate.delete("lunbo");//轮播图
        redisTemplate.delete("tuijian"+2);//推荐
        redisTemplate.delete("like"+4);//喜欢
        List<Long> arrs = JSONObject.parseArray(ids, Long.class);//将传过来的String类型的json转换为list集合
        tts.deletearr(arrs);
        return R.ok();
    }




    //图片上传
    @RequestMapping("upload")
    @ResponseBody
    public R upload(@RequestParam("image") MultipartFile image){
        String http="https://";

        // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1（杭州）为例，Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
        String endpoint = "oss-cn-beijing.aliyuncs.com";
        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId ="LTAI5tJG9udR6TFeyPkPiSxb" ;
        String accessKeySecret = "UG5PcFu7GxGDMAKm4W7qOeURpBIzfn";
        // 填写Bucket名称，例如examplebucket。
        String bucketName = "dk2202-w";
        String imagedir="image/";
        // 填写文件名。文件名包含路径，不包含Bucket名称。例如exampledir/exampleobject.txt。
        String objectName = imagedir+image.getOriginalFilename();
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(http+endpoint, accessKeyId, accessKeySecret);
        try {
            ossClient.putObject(bucketName, objectName, image.getInputStream() );
        }catch (Exception oe){
        }finally {
            if(ossClient !=null ){
                ossClient.shutdown();// 关闭OSSClient。
            }
        }
//        https://dk2202-w.oss-cn-beijing.aliyuncs.com/image/c_0001.jpg
        StringBuffer url=new StringBuffer();
        url.append(http);
        url.append(bucketName);
        url.append(".");
        url.append(endpoint);
        url.append("/");
        url.append(objectName);
        String s=url.toString();//将上传成功的路径返回前台
        return R.ok().setData(s);
    }




}
